package com.ron.android.daneel;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.net.ContentURI;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.Menu.Item;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import com.ron.provider.Daneel;
import com.ron.provider.Daneel.Notes;

public class NoteListView extends ListActivity {

	private static final int MENU_NEW_ACTION = 1;
	private static final int MENU_EDIT_ACTION = 2;
	private static final int MENU_DELETE_ACTION = 3;
	private static final int MENU_FILTER_ACTION = 4;
	private static final int MENU_CLEAR_FILTER_ACTION = 5;
	private static final int MENU_SEPARATOR = 99;

	private static final int SET_FILTER_ACTIVITY = 1;

	private String filterExpression = null;
	private String containsFilterText = null;
	private String notContainsFilterText = null;

	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		setContentView(R.layout.note_list_view);

		Intent intent = getIntent();
		if (intent.getData() == null) {
			intent.setData(Daneel.Notes.CONTENT_URI);
		}

		populate();
	}

	private void populate() {
		Cursor cursor = managedQuery(getIntent().getData(),
				Notes.DEFAULT_PROJECTION, filterExpression, null);

		ListAdapter adapter = new SimpleCursorAdapter(this,
				R.layout.list_view_row, cursor,
				new String[] { Daneel.Notes.TEXT },
				new int[] { R.id.view_row_text });

		setListAdapter(adapter);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);

		menu.add(0, MENU_NEW_ACTION, R.string.menu_new_note);
		menu.add(0, MENU_EDIT_ACTION, R.string.menu_edit_note);
		menu.add(0, MENU_DELETE_ACTION, R.string.menu_delete_note);
		menu.addSeparator(0, MENU_SEPARATOR);
		menu.add(0, MENU_FILTER_ACTION, R.string.menu_filter);
		menu.add(0, MENU_CLEAR_FILTER_ACTION, R.string.menu_clear_filter);

		updateOptionsMenuVisibility(menu);

		return true;
	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		super.onPrepareOptionsMenu(menu);
		updateOptionsMenuVisibility(menu);
		return true;
	}

	private void updateOptionsMenuVisibility(Menu menu) {
		menu.setItemShown(MENU_EDIT_ACTION,
				this.getListAdapter().getCount() > 0);
		menu.setItemShown(MENU_DELETE_ACTION,
				this.getListAdapter().getCount() > 0);
	}

	@Override
	public boolean onOptionsItemSelected(Item item) {
		switch (item.getId()) {
		case MENU_NEW_ACTION:
			insertItem();
			break;
		case MENU_EDIT_ACTION:
			if (getSelectionRowID() > 0) {
				editItem(getIntent().getData().addId(getSelectionRowID()));
			}
			break;
		case MENU_DELETE_ACTION:
			if (getSelectionRowID() > 0) {
				deleteItem(getIntent().getData().addId(getSelectionRowID()));
			}
			break;
		case MENU_FILTER_ACTION:
			Intent filterIntent = new Intent(Daneel.SET_FILTER_ACTION);
			filterIntent.putExtra(Daneel.TEXT_CONTAINS_KEY, containsFilterText);
			filterIntent.putExtra(Daneel.TEXT_NOT_CONTAINS_KEY, notContainsFilterText);
			startSubActivity( filterIntent, SET_FILTER_ACTIVITY);
			break;
		case MENU_CLEAR_FILTER_ACTION:
			containsFilterText = null;
			notContainsFilterText = null;
			setFilter();
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		ContentURI clickedURI = getIntent().getData().addId(id);
		editItem(clickedURI);
		super.onListItemClick(l, v, position, id);
	}

	private void insertItem() {
		startActivity(new Intent(Intent.INSERT_ACTION, getIntent().getData()));
	}

	private void editItem(ContentURI uri) {
		startActivity(new Intent(Intent.EDIT_ACTION, uri));
	}

	private void deleteItem(ContentURI uri) {
		getContentResolver().delete(uri, null, null);

		// TODO: why do I have to do this? All the other CRUD methods
		// don't need to call populate to get the screen to refresh.
		// AFAIK, the delete method in DaneelProvider is notifying
		// correctly, but that could be where the problem lies...
		populate();
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode,
			String data, Bundle extras) {
		super.onActivityResult(requestCode, resultCode, data, extras);

		switch (requestCode) {
		case SET_FILTER_ACTIVITY:
			if (resultCode == Daneel.SUBACTIVITY_SUCEEDED) {
				containsFilterText = extras.getString(Daneel.TEXT_CONTAINS_KEY);
				notContainsFilterText = extras.getString(Daneel.TEXT_NOT_CONTAINS_KEY);
				setFilter();
			}
			break;
		default:
			break;
		}

	}

	private void setFilter() {
		filterExpression = null;

		if ((containsFilterText != null) && (notContainsFilterText == null)) {
			filterExpression = "text like '%" + containsFilterText + "%'";
		} else if ((containsFilterText == null) && (notContainsFilterText != null)) {
			filterExpression = "text not like '%" + notContainsFilterText + "%'";
		} else if ((containsFilterText != null) && (notContainsFilterText != null)) {
			filterExpression = "text like '%" + containsFilterText
					+ "%' and text not like '%" + notContainsFilterText + "%'";
		}

		populate();
	}

}